package com.enwayouli.controller;

import com.enwayouli.mapper.BlogMapper;
import com.enwayouli.mapper.MdMapper;
import com.enwayouli.pojo.Blog;
import com.enwayouli.pojo.Md;
import com.enwayouli.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@ResponseBody
public class BlogController {

    private MdMapper mdMapper;
    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    public void setMdMapper(MdMapper mdMapper) {
        this.mdMapper = mdMapper;
    }

    @Autowired
    public void setBlogMapper(BlogMapper blogMapper) {
        this.blogMapper = blogMapper;
    }

    private static final String INSIDE_IMG_PATH = "/www/wwwroot/www.enwayouli.top/dist/img/";
    private static final String COVER_IMG_PATH = "/www/wwwroot/www.enwayouli.top/dist/coverImg/";

    /**
     * upload method
     * @param file
     * @param pathName
     * @return file path
     */
    private String upload(MultipartFile file,String pathName,String imgPath){
        if (file.isEmpty()) {
            System.out.println("文件为空");
            return "文件为空";
        }
        String FileName = file.getOriginalFilename();
        String suffixName = FileName.substring(FileName.lastIndexOf("."));

        FileName = UUID.randomUUID() + suffixName;
        File dest = new File(pathName + FileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "https://www.enwayouli.top/" + imgPath + FileName;
    }

    @RequestMapping("/upload")
    public String picUpload(@RequestParam(value = "image") MultipartFile file){
        return  upload(file,INSIDE_IMG_PATH,"img/");
    }
    @RequestMapping("/remove")
    public boolean remove(String imgUrl){
        if (imgUrl==null) {
            return false;
        }
        File file = new File(imgUrl);
        return file.delete();
    }

    @RequestMapping(value = "/blogUpload", consumes = "multipart/form-data")
    public boolean blogUpload(@ModelAttribute Blog blog,@RequestParam(value = "myFile")MultipartFile file,@RequestParam("uid") int uid,@RequestParam("md") String md){
        try {
            //上传后的封面图片路径
            String coverPath = upload(file,COVER_IMG_PATH,"coverImg/");

            Md MyMd = new Md(0,md);
            mdMapper.addMd(MyMd);
            blog.setCoverUrl(coverPath);
            blog.setMid(MyMd.getMid());
            blog.setUser(new User(uid));
            blogMapper.addBlog(blog);
        } catch (Exception e){
            return false;
        }
        return true;
    }

    @RequestMapping("/queryAllBlog")
    public List<Blog> getALLBlog(){
        return blogMapper.queryAll();
    }
    @RequestMapping("/getMd")
    public Md getMdById(int mid){
        return mdMapper.getMdById(mid);
    }

    @RequestMapping("/blogFilter")
    public List<Blog> blogFilter(HttpServletRequest request) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map map = new HashMap();
        String title = request.getParameter("title").trim();
        String intro = request.getParameter("intro").trim();
        String username = request.getParameter("username").trim();
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        if (title != null && !title.trim().equals("")) {
            map.put("title",title);
        }
        if (intro != null && !intro.trim().equals("")) {
            map.put("intro",intro);
        }
        if (username != null && !username.trim().equals("")) {
            map.put("username",username);
        }
        if (startDate != null) {
            map.put("startDate",sdf.parse(startDate));
        }
        if (endDate != null) {
            map.put("startDate",sdf.parse(endDate));
        }
        return blogMapper.blogFilter(map);
    }

    @RequestMapping("/getBlogNum")
    public int getBlogNum(){
        return blogMapper.getNum();
    }

    @RequestMapping("/addView")
    public int addView(int mid){
        return blogMapper.addView(mid);
    }

    @RequestMapping("/getTop5Blog")
    public List<Blog> getTop5Blog(){
        return blogMapper.getTop5Blog();
    }
}
